from django.shortcuts import render,redirect
from db import models
import time
# Create your views here.
def projectlist(request):
    user_name = request.session.get("user_name")
    if request.method == "GET":
        project_list = models.projectinfo.objects.values()
        info = {"user_name": user_name, "project_list": project_list}
        return render(request, "other/projectlist.html", info)
    else:
        pass
def projectadd(request):
    user_name = request.session.get("user_name")
    if request.method == "GET":
        info = {"user_name": user_name,}
        return render(request, "other/projectadd.html", info)
    else:
        ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        project_name =  request.POST.get("project_name")
        project_alias = request.POST.get("project_alias")
        project_status = models.projectinfo.objects.filter(project_name=project_name).count()
        if project_status == 0:
            models.projectinfo.objects.create(project_name=project_name,project_alias=project_alias,project_ctime=ctime)
            models.log_record.objects.create(user_name=user_name, log_content="添加项目%s" % (project_alias),
                                             log_ctime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        return redirect("/project/projectlist")

def projectdel(request):
    user = request.session.get("user_name")
    if request.method == "GET":
        id = request.GET.get("id")
        project_name = models.projectinfo.objects.filter(project_id=id).values("project_alias")[0]
        models.projectinfo.objects.filter(project_id=id).delete()
        models.log_record.objects.create(user_name=user, log_content="删除项目%s" % (project_name),
                                         log_ctime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

        return redirect("/project/projectlist/")

def projectupdate(request):
    pass